package com.gopay.bis.service.bank.impl.ebank;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.BisPayCheckUtils;
import com.gopay.bis.common.util.constants.BisPayConstants;
import com.gopay.bis.manager.bank.ebank.CmbcB2cManager;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.CmbcConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.cmbc.ebank.CmbcMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankInteractRemoteService;

public class CmbcB2cBankServiceImpl implements BankInteractRemoteService {
	
	@Resource(name = "cmbcB2cManager")
	private CmbcB2cManager cmbcB2cManager;
	
	@Override
	public Result pay(BaseMessage base) {
		Result result = new Result(false);
		try {
			CmbcMessage messageB2C = new CmbcMessage();
			BeanUtils.copyProperties(base, messageB2C);
			messageB2C = (CmbcMessage) cmbcB2cManager.sign(messageB2C);
			result.setDefaultModel(messageB2C);
			result.setSuccess(true);
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6029);
		}
		return result;
	}

	@Override
	public Result bankBack(BaseMessage base) {
		Result result = new Result(false);
		try {
			boolean flag = false;
			CmbcMessage messageB2C = new CmbcMessage();
			BeanUtils.copyProperties(base, messageB2C);
			HashMap map = (HashMap) cmbcB2cManager.verify(messageB2C);
			messageB2C = (CmbcMessage)map.get("cmbcMessage");
			flag = (Boolean)map.get("flag");
			result.setDefaultModel(flag);
			result.setSuccess(true);
			result.setModel("res", messageB2C);
			if (flag) {
				if (messageB2C.getOrderStatus().equals(CmbcConstants.ORDER_STATE_SUCCESS)) {
					// 通知后台地址入账
					Map<String, String> param = new HashMap<String, String>();
					param.put("orderId", messageB2C.getOrderId());
					param.put("amt", messageB2C.getAmount().toString());
					param.put("cardType", "Unknown");
					param.put("notifyNum", "1");
					param.put("payStatus", "00");
					param.put("retCode", "00");
					param.put("retMsg", "验签成功");
					param.put("signData", messageB2C.getSignMessage());
					param.put("isFront", String.valueOf(base.isFront()));
					result.setModel("isNotifyCashier", param);
					 result.setOrderStatus(true);
				} else {
					// 通知后台地址该订单失败
					Map<String, String> param = new HashMap<String, String>();
					param.put("orderId", messageB2C.getOrderId());
					param.put("amt", messageB2C.getAmount().toString());
					param.put("cardType", "Unknown");
					param.put("notifyNum", "1");
					param.put("payStatus", "01");
					param.put("retCode", "01");
					param.put("retMsg", messageB2C.getResMessage());
					param.put("signData", messageB2C.getSignMessage());
					param.put("isFront", String.valueOf(base.isFront()));
					result.setModel("isNotifyCashier", param);
					 result.setOrderStatus(false);
				}
			}
		
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6064);
		}
		return result;
	}

	@Override
	public Result query(BaseMessage base) {
		Result result = new Result(false);
		try {
			CmbcMessage messageB2C = new CmbcMessage();
			BeanUtils.copyProperties(base, messageB2C);
			CmbcMessage res = (CmbcMessage) cmbcB2cManager.query(messageB2C);
			result.setDefaultModel(res);
			result.setSuccess(true);
			if(BisPayCheckUtils.checkExternalOrderID(messageB2C.getOrderId())){
				Map<String, String> param = new HashMap<String, String>();
				param.put("orderId", messageB2C.getOrderId());
				param.put("amount", messageB2C.getAmount().toString());
				if (res == null) {
					// 查询订单为空
					param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
					param.put("returnMessage", "银行返回对账结果为空");
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					result.setModel("queryResult", param);
					return result;
				}
				if ((res.getOrderAmt() == null || res.getOrderNo() == null || res.getOrderStatus() == null) && res.getRemark() != null) {
					// 查询订单错误
					param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
					param.put("returnMessage", "银行订单查询失败");
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					result.setModel("queryResult", param);
					return result;
				}
				if (res.getOrderStatus().equals(CmbcConstants.QUERY_ORDER_FAIL)) {
					param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
					param.put("returnMessage", res.getResMessage());
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
				} else if (res.getOrderStatus().equals(CmbcConstants.QUERY_ORDER_SUCC)) {
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);						
					param.put("tradeState", BisPayConstants.TRADESTATE_SUCCESS);
					param.put("returnMessage", "交易成功");
				} else if (res.getOrderStatus().equals(CmbcConstants.QUERY_ORDER_EXCEP)) {
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);						
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					param.put("returnMessage", "订单状态异常！请联系银行");
				} else if (res.getOrderStatus().equals(CmbcConstants.QUERY_ORDER_BANKFAIL)) {
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);						
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					param.put("returnMessage", "银行后台查询失败");
				} else if (res.getOrderStatus().equals(CmbcConstants.QUERY_ORDER_DBERROR)) {
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);						
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					param.put("returnMessage", "银行数据库错误");
				} else if (res.getOrderStatus().equals(CmbcConstants.QUERY_ORDER_NOFIND)) {
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);						
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					param.put("returnMessage", "银行订单不存在");
				} else {
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);						
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					param.put("returnMessage", "交易发生异常！跳出");
				}
				result.setModel("queryResult", param);
			}
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6033);
		}
		return result;
	}

	@Override
	public Result refund(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result dayend(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result queryBack(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

    @Override
    public Result refundBack(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result refundQuery(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

	@Override
	public Result sendBankRefund(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

}
